/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace KeyboardBlinker
{
	/// <summary>
	/// Description of KeyboardBlinker.
	/// </summary>
	public class BlinkerClass
    {
    	Thread BlinkThread;
        const int VK_SCROLL = 0x91;
        const int KEYUP = 0x2;
        [DllImport("user32.dll")]
        static extern IntPtr keybd_event(IntPtr KEY, IntPtr bScan, IntPtr dwFlags, IntPtr dwExtraInfo);
        [DllImport("user32.dll")]
        static extern IntPtr GetKeyState(IntPtr KEY);
        ~BlinkerClass()
		{
			StopBlink();
		}
        private void Blink()
        {
            int scroll0 = (int)GetKeyState(new IntPtr(VK_SCROLL));
            while (!aborted)
            {
            	keybd_event(new IntPtr(VK_SCROLL), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                //Thread.Sleep(1);
                keybd_event(new IntPtr(VK_SCROLL), IntPtr.Zero, new IntPtr(KEYUP), IntPtr.Zero);
                Thread.Sleep(250);

            }
            //keybd_event(new IntPtr(VK_SCROLL), IntPtr.Zero, new IntPtr(KEYUP), IntPtr.Zero);
            if (scroll0 != (int)GetKeyState(new IntPtr(VK_SCROLL)))
            {
                keybd_event(new IntPtr(VK_SCROLL), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                Thread.Sleep(1);
                keybd_event(new IntPtr(VK_SCROLL), IntPtr.Zero, new IntPtr(KEYUP), IntPtr.Zero);
            }
        }
        public void StartBlink()
        {
        	if (BlinkThread == null || BlinkThread.ThreadState == System.Threading.ThreadState.Aborted || BlinkThread.ThreadState == System.Threading.ThreadState.Stopped)
			{
				aborted = false;
				BlinkThread = new Thread(new ThreadStart(Blink));
				BlinkThread.Start();
			}
        }
        public void StopBlink()
        {
        	while (BlinkThread != null && (BlinkThread.ThreadState != System.Threading.ThreadState.Stopped))
			{
				aborted=true;
				Thread.Sleep(50);
			}
        }
        public bool aborted=false;
        public enum BlinkKeys: int
        {
        	SCROLL = 0x91
        }
        public BlinkKeys BlinkKey;
    }
    public class NewBlinker
    {
    	Thread BlinkThread;
        [DllImport("NTKbdLites.dll")]
        static extern IntPtr OpenKeyboardDevice(out IntPtr error);
        [DllImport("NTKbdLites.dll")]
        static extern IntPtr CloseKeyboardDevice(IntPtr KbdHandle);
        [DllImport("NTKbdLites.dll")]
        static extern IntPtr FlashKeyboardLight(IntPtr KbdHandle, uint LEDFLAG, int Duration);
        public const uint KEYBOARD_CAPS_LOCK_ON   =  4;
		public const uint KEYBOARD_NUM_LOCK_ON    =  2;
		public const uint KEYBOARD_SCROLL_LOCK_ON =  1;
		~NewBlinker()
		{
			StopBlink();
		}
        private void Blink()
        {
			IntPtr KbdHandle,ErrNum = IntPtr.Zero;
			KbdHandle = OpenKeyboardDevice(out ErrNum);
            while (!aborted)
            {
                
            	FlashKeyboardLight(KbdHandle,BlinkKey,BlinkDuration);
                Thread.Sleep(realblinkdur-BlinkDuration);
            }
            CloseKeyboardDevice(KbdHandle);
        }
        public void StartBlink(uint BlinkKey,int BlinkDuration)
        {
        	this.BlinkKey = BlinkKey;
        	this.BlinkDuration = 5;
            realblinkdur = BlinkDuration;
        	if (BlinkThread == null || BlinkThread.ThreadState == System.Threading.ThreadState.Aborted || BlinkThread.ThreadState == System.Threading.ThreadState.Stopped)
			{
				aborted = false;
				BlinkThread = new Thread(new ThreadStart(Blink));
				BlinkThread.Start();
			}
        }
        public void StopBlink()
        {
        	while (BlinkThread != null && (BlinkThread.ThreadState != System.Threading.ThreadState.Stopped))
			{
				aborted=true;
				Thread.Sleep(50);
			}
        }
        public bool aborted=false;
        public uint BlinkKey=KEYBOARD_SCROLL_LOCK_ON;
        public int BlinkDuration=250;
        private int realblinkdur;
    }
}
